<template>
  <div class="main">
    <div class="title2">
      <span @click="onMessgae">信息修改</span>
    </div>
    <form :model="userinfo" @submit.prevent="onSubmit">
      <div class="form-group">
        <label>姓名:</label>
        <input type="text" v-model="userinfo.name" />
      </div>
      <div class="form-group">
        <label>年龄:</label>
        <input type="text" v-model="userinfo.age" />
      </div>
      <div class="form-group">
        <label>手机:</label>
        <input type="tel" v-model="userinfo.phone" />
      </div>
      <div class="form-group">
        <label>邮箱:</label>
        <input type="text" v-model="userinfo.email" />
      </div>
      <button style="float: right" type="submit">Submit</button>
    </form>
  </div>
</template>
<script setup>
import { onMounted } from 'vue'
import { useUserStore } from '@/stores/axios-user'
import { storeToRefs } from 'pinia'

const store = useUserStore()
const { userinfo } = storeToRefs(store)

onMounted(() => {
  store.getInfo().then((result) => alert(result.status))
})
const onSubmit = () => {
  store.editInfo((res) => {
    if (res) {
      alert('修改成功')
    } else alert('修改失败')
  })
}

import framePost from '@/utils/framePost'
const iframe = framePost((msg) => {
  alert(msg)
  //let res = JSON.parse(msg)
  //alert('status:' + res.status + ' name:' + res.data.name)
})
const onMessgae = () => {
  var data = { type: 'get', url: 'http://thinktest/api/Test/user', data: userinfo }
  iframe.sendMessage(JSON.stringify(data), '*')
}
</script>

<style>
.main {
  margin: 0 auto;
  padding-left: 25px;
  padding-right: 25px;
  padding-top: 15px;
  width: 350px;
  height: 350px;
  background: #ffffff;
  /*以下css用于让登录表单垂直居中在界面,可删除*/
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -175px;
  margin-left: 15px;
}
.title2 {
  width: 100%;
  height: 40px;
  line-height: 40px;
  margin-bottom: 10px;
  text-align: center;
}

.title2 span {
  font-size: 18px;
  color: #353f42;
}
.form-group {
  width: 100%;
}
.form-group input {
  width: 100%;
  height: 30px;
  border: 1px solid #dad9d6;
  background: #ffffff;
  margin: 10px 0;
}
button {
  width: 30%;
  height: 30px;
  margin: 10px 0;
}
</style>
@/stores/axios-user
